package xhwl.hmo.mall.views;

import android.content.Context;
import android.util.AttributeSet;
import android.webkit.WebView;

/**
 * Created by Administrator on 2017/11/14.
 */

public class NewWebView extends WebView {

    private OnScrollChangeListener mOnScrollChangeListener;
    boolean isTop = false;

    public NewWebView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        super.onScrollChanged(l, t, oldl, oldt);
        // webview的高度
        float webcontent = getContentHeight() * getScale();
        // 当前webview的高度
        float webnow = getHeight() + getScrollY();
        if (Math.abs(webcontent - webnow) < 1) {
            //处于底端
            mOnScrollChangeListener.onPageEnd(l, t, oldl, oldt);
        } else if (getScrollY() == 0) {
            //处于顶端
            isTop = true;
            mOnScrollChangeListener.onPageTop(l, t, oldl, oldt);
        } else {
            isTop = false;
            mOnScrollChangeListener.onScrollChanged(l, t, oldl, oldt);
        }
    }

    public void setOnScrollChangeListener(OnScrollChangeListener listener) {
        this.mOnScrollChangeListener = listener;
    }

    public interface OnScrollChangeListener {

        public void onPageEnd(int l, int t, int oldl, int oldt);

        public void onPageTop(int l, int t, int oldl, int oldt);

        public void onScrollChanged(int l, int t, int oldl, int oldt);
    }

    float x1 = 0;
    float y1 = 0;

//    @Override
//    public boolean onTouchEvent(MotionEvent event) {
//        switch (event.getAction()) {
//            case MotionEvent.ACTION_UP:
////                Log.e("=====up", "59行");
////                this.getParent().getParent().requestDisallowInterceptTouchEvent(false);
////                this.getParent().requestDisallowInterceptTouchEvent(false);
////                this.requestDisallowInterceptTouchEvent(true);
//                break;
//            case MotionEvent.ACTION_DOWN:
//                x1 = event.getX();
//                y1 = event.getY();
////                if (isTop) {
////                    Log.e("=====down", "isTop");
////                    getParent().requestDisallowInterceptTouchEvent(true);
////                    this.requestDisallowInterceptTouchEvent(false);
////                } else {
////                    Log.e("=====down", "!isTop");
////                    getParent().requestDisallowInterceptTouchEvent(false);
////                    this.requestDisallowInterceptTouchEvent(true);
////                }
//                break;
//            case MotionEvent.ACTION_MOVE:
//                float x2 = event.getX();
//                float y2 = event.getY();
//                if (x2 == x1 || Math.abs(y2 - y1) > Math.abs(x2 - x1)) { //竖直方向移动
//                    if (y2-y1>0){//向下滑动
//                        if (isTop) {
//                            Log.e("=====down", "isTop");
//                            getParent().requestDisallowInterceptTouchEvent(true);
//                            this.requestDisallowInterceptTouchEvent(false);
//                        } else {
//                            Log.e("=====down", "!isTop");
//                            getParent().requestDisallowInterceptTouchEvent(false);
//                            this.requestDisallowInterceptTouchEvent(true);
//                        }
//
//                    }else{
//                        Log.e("=====up", "向上");
////                        this.getParent().getParent().requestDisallowInterceptTouchEvent(false);
////                        this.getParent().requestDisallowInterceptTouchEvent(false);
//                        this.requestDisallowInterceptTouchEvent(true);
//                    }
//                } else {
//                }
//                break;
//        }
//        return super.onTouchEvent(event);
//    }
}
